<template>
  <div v-loading="loading" style="width: 100%;height: 100%">
    <iframe v-if="loginUrl !== null && loginUrl !== ''" class="custom-iframe" :src="loginUrl" />
  </div>
</template>

<script>
import { initByDomain } from '@/api/portal/user/cas';
export default {
  name: 'DialogUtils',
  data() {
    return {
      loading: false,
      loginUrl: null,
      redirect: null
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.redirect !== null && undefined !== this.redirect) {
        let domain = this.redirect.replace('https://', '').replace('http://', '');
        domain = domain.substring(0, domain.indexOf('/'));
        initByDomain({ code: domain }).then(res => {
          const { data } = res;
          this.loginUrl = data.src;
          setTimeout(() => {
            this.isIframe = true;
            this.loading = false;
            window.location.open(this.redirect);
          }, 1000);
        }).catch(() => {
          this.loading = false;
        });
      } else {
        this.loading = false;
      }
    }
  }
};
</script>

<style scoped>
.custom-iframe {
  width: 100%;
  height: 0px;
  overflow: hidden;
}
</style>
